//
//  UIView+Addition.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import Foundation
import UIKit
extension UIView {
    
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
    
    @IBInspectable var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }
    
    @IBInspectable var borderColor: UIColor? {
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            layer.borderColor = newValue?.cgColor
        }
    }
    
    @IBInspectable var shadowColor: UIColor? {
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            layer.shadowColor = newValue?.cgColor
        }
    }
    
    @IBInspectable var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }
    
    @IBInspectable var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }
    
    @IBInspectable var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }
    func addRoundCorners(_ corners: UIRectCorner, radius: CGFloat) {
           let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
           let shapeLayer = CAShapeLayer()
           shapeLayer.frame = bounds
           shapeLayer.path = maskPath.cgPath
           layer.mask = shapeLayer
       }
    
    func addBorderAndRadius(corners: UIRectCorner, radius: CGFloat, width: CGFloat, color: UIColor) {
            let maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds
            
            let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            maskLayer.path = maskPath.cgPath
            
            self.layer.mask = maskLayer
            
            let borderLayer = CAShapeLayer()
            borderLayer.path = maskPath.cgPath
            borderLayer.fillColor = UIColor.clear.cgColor
            borderLayer.strokeColor = color.cgColor
            borderLayer.lineWidth = width
            borderLayer.frame = self.bounds
            self.layer.addSublayer(borderLayer)
        }
    
    
    func addRoundCorners(_ corners: UIRectCorner, radius: CGFloat, borderWidth: CGFloat, borderColor: UIColor) {
          let maskPath = UIBezierPath(roundedRect: self.bounds,
                                      byRoundingCorners: corners,
                                      cornerRadii: CGSize(width: radius, height: radius))
          
          let maskLayer = CAShapeLayer()
          maskLayer.frame = self.bounds
          maskLayer.path = maskPath.cgPath
          self.layer.mask = maskLayer
          
          let borderLayer = CAShapeLayer()
          borderLayer.path = maskPath.cgPath
          borderLayer.lineWidth = borderWidth
          borderLayer.strokeColor = borderColor.cgColor
          borderLayer.fillColor = UIColor.clear.cgColor
          self.layer.addSublayer(borderLayer)
      }
      
      func addRoundCorners(topLeftRadius: CGFloat, topRightRadius: CGFloat, bottomLeftRadius: CGFloat, bottomRightRadius: CGFloat, borderWidth: CGFloat, borderColor: UIColor) {
          // 先移除旧的 borderLayer
             if let sublayers = self.layer.sublayers {
                 for layer in sublayers {
                     if layer is CAShapeLayer {
                         layer.removeFromSuperlayer()
                     }
                 }
             }
          let maskPath = UIBezierPath()
          
          maskPath.move(to: CGPoint(x: 0, y: topLeftRadius))
          maskPath.addArc(withCenter: CGPoint(x: topLeftRadius, y: topLeftRadius),
                          radius: topLeftRadius,
                          startAngle: .pi,
                          endAngle: .pi * 1.5,
                          clockwise: true)
          
          maskPath.addLine(to: CGPoint(x: self.bounds.width - topRightRadius, y: 0))
          maskPath.addArc(withCenter: CGPoint(x: self.bounds.width - topRightRadius, y: topRightRadius),
                          radius: topRightRadius,
                          startAngle: .pi * 1.5,
                          endAngle: 0,
                          clockwise: true)
          
          maskPath.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height - bottomRightRadius))
          maskPath.addArc(withCenter: CGPoint(x: self.bounds.width - bottomRightRadius, y: self.bounds.height - bottomRightRadius),
                          radius: bottomRightRadius,
                          startAngle: 0,
                          endAngle: .pi * 0.5,
                          clockwise: true)
          
          maskPath.addLine(to: CGPoint(x: bottomLeftRadius, y: self.bounds.height))
          maskPath.addArc(withCenter: CGPoint(x: bottomLeftRadius, y: self.bounds.height - bottomLeftRadius),
                          radius: bottomLeftRadius,
                          startAngle: .pi * 0.5,
                          endAngle: .pi,
                          clockwise: true)
          
          maskPath.close()
          
          let maskLayer = CAShapeLayer()
          maskLayer.frame = self.bounds
          maskLayer.path = maskPath.cgPath
          self.layer.mask = maskLayer
          
          let borderLayer = CAShapeLayer()
          borderLayer.path = maskPath.cgPath
          borderLayer.lineWidth = borderWidth
          borderLayer.strokeColor = borderColor.cgColor
          borderLayer.fillColor = UIColor.clear.cgColor
          self.layer.addSublayer(borderLayer)
      }
    
    func addDashedBorder(color: UIColor = .black, lineWidth: CGFloat = 1, cornerRadius: CGFloat = 0) {
            // 在视图的 layoutSubviews() 方法中执行以下代码
            layer.sublayers?.filter { $0.name == "DashedBorderLayer" }.forEach { $0.removeFromSuperlayer() }
            let shapeLayer = CAShapeLayer()
            shapeLayer.name = "DashedBorderLayer"
            shapeLayer.strokeColor = color.cgColor
            shapeLayer.lineWidth = lineWidth
            shapeLayer.lineDashPattern = [4, 4] // 虚线长度和间隔的设置
            shapeLayer.fillColor = nil
            let insetRect = bounds.insetBy(dx: lineWidth / 2 + 1, dy: lineWidth / 2 + 1)
            let path = UIBezierPath(roundedRect: insetRect, cornerRadius: cornerRadius)
            shapeLayer.path = path.cgPath
            layer.addSublayer(shapeLayer)
        }
        
//    public override func layoutSubviews() {
//            super.layoutSubviews()
//            addDashedBorder()
//        }

    func asImage() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0.0)
        defer { UIGraphicsEndImageContext() }
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        return image
    }
    func addHexagonalMask(borderColor: UIColor, borderWidth: CGFloat) {
        // 创建一个六边形的贝塞尔路径
        let path = UIBezierPath()
        let size = bounds.size
        
        let lineWidth = borderWidth / sqrt(3) // 六边形边线宽度
        
        let height = size.height
        let width = size.width
        let side = min(height - 2 * lineWidth, width) // 六边形边长
        
        let x = (width - side) / 2
        let y = (height - side) / 2
        
        path.move(to: CGPoint(x: x + side / 2, y: y))
        path.addLine(to: CGPoint(x: x + side, y: y + side * 0.25))
        path.addLine(to: CGPoint(x: x + side, y: y + side * 0.75))
        path.addLine(to: CGPoint(x: x + side / 2, y: y + side))
        path.addLine(to: CGPoint(x: x, y: y + side * 0.75))
        path.addLine(to: CGPoint(x: x, y: y + side * 0.25))
        path.close()
        
        // 创建一个 CAShapeLayer，并将路径设置为它的 path
        let maskLayer = CAShapeLayer()
        maskLayer.path = path.cgPath
        
        // 将 maskLayer 设置为当前视图的 mask
        layer.mask = maskLayer
        
        // 创建一个边框路径
        let borderPath = UIBezierPath(cgPath: path.cgPath)
        borderPath.lineWidth = borderWidth
        
        // 创建一个边框 CAShapeLayer，并将路径设置为它的 path
        let borderLayer = CAShapeLayer()
        borderLayer.path = borderPath.cgPath
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.fillColor = UIColor.clear.cgColor
        
        // 将边框 CAShapeLayer 添加到当前视图的 layer 上
        layer.addSublayer(borderLayer)
    }
}

